package com.servlet.service;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import com.servlet.dao.UserDao;
import com.servlet.vo.User;

public class UserService {

	private boolean flag = false;
	
	public boolean add(HttpServletRequest request) throws SQLException{
		
		User user = new User();
		user.setUserName(request.getParameter("userName"));
		user.setPassword(request.getParameter("password"));
		user.setTrueName(request.getParameter("trueName"));
		
		flag = new UserDao().save(user);
		
		return flag;
	}
	
	public boolean del(HttpServletRequest request) throws SQLException{
		
		String objId = request.getParameter("objId");
		if(null != objId && !"".equals(objId)){

			flag = new UserDao().delete(Long.parseLong(objId));
		}
		
		return flag;
	}
	
	public boolean update(HttpServletRequest request) throws SQLException{
		
		User user = new User();
		user.setId(Long.parseLong( request.getParameter("objid")));
		user.setUserName(request.getParameter("userName"));
		user.setPassword(request.getParameter("password"));
		user.setTrueName(request.getParameter("trueName"));
		
		flag = new UserDao().update(user);
		return flag;
	}

	/**
	 * 登陆方法， 
	 * 后期添加 md5密码加密方法，预防注入攻击
	 * 
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	public boolean load(HttpServletRequest request) throws SQLException{
		
		String userName = request.getParameter("userName");
		String passwd = request.getParameter("passwd");
		User user = new UserDao().getLoadUser(userName, passwd);
		if(null != user){
			flag = true;
			request.getSession().setAttribute("USERSESSION", user);
		}
		return flag;
	}
	
}
